package com.ygo.feihua.adapter;

import android.content.*;
import android.support.v7.widget.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import cn.bmob.v3.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.util.*;
import com.yuyh.library.imgsel.common.*;
import java.util.*;
import android.util.*;

public class FatieTypeAdapter extends RecyclerView.Adapter<FatieTypeAdapter.ViewHolder>
{

	public static final int FATIE_SELECT=0;
	public static final int FATIE_TYPE=1;

	DialogUtils du;
	
	@Override
	public int getItemViewType(int position)
	{
		if(data.get(position).getName().equals("")){
			return FATIE_SELECT;
		}else{
			return FATIE_TYPE;
		}
	}
	
	public List<TieType> getData(){
		return data;
	}
	
	@Override
	public FatieTypeAdapter.ViewHolder onCreateViewHolder(ViewGroup p1, int p2)
	{
		View v=LayoutInflater.from(p1.getContext()).inflate(R.layout.item_ft_type, p1,false);
		ViewHolder vh=new ViewHolder(v)  ;
		return vh;
	}

	@Override
	public void onBindViewHolder(FatieTypeAdapter.ViewHolder vh, final int position)
	{
		if(getItemViewType(position)==FATIE_SELECT){
			vh.ift_icon.setVisibility(View.GONE);
			vh.ift_name.setText("点击选择分类");
		}else{
			vh.ift_icon.setVisibility(View.VISIBLE);
			TieType tp=data.get(position);
			vh.ift_name.setText(tp.getName());
			gj.tuxian(tp.getIcon(),vh.ift_icon);
		}
		vh.v.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					if(position==0){
						tie_type("选择分类",null,position,false);
					}else{
						
						tie_type(data.get(position).getName(),data.get(position),position,true);
						
					}
					// TODO: Implement this method
				}
			});
		// TODO: Implement this method
	}

	@Override
	public int getItemCount()
	{
		
		// TODO: Implement this method
		return data.size();
	}

	public void sx(){
		notifyDataSetChanged();
	}

	private List<TieType> data;
	private Context context;
	
	private Gongju gj;

	public FatieTypeAdapter(Context context,List<TieType> data){
		this.context=context;
		this.data=data;
		gj=Gongju.getdx(context);
		du=DialogUtils.getdx(context);
	}

	public class ViewHolder extends RecyclerView.ViewHolder{
		
		ImageView ift_icon;
		TextView ift_name;
		View v;
		
		public ViewHolder(View v){
			super(v);
			this.v=v;
			ift_icon=(ImageView) v.findViewById(R.id.ift_icon);
			ift_name=(TextView) v.findViewById(R.id.ift_name);
		}
		
	}
	public void tie_type(final String title,final TieType tt,final int position,final boolean isNext)
	{
		du.dialogj1(false,"","加载中,请稍等");
		
		BmobQuery<TieType> bq=new BmobQuery<TieType>();
		bq.order("-createdAt");
		
		if(position==data.size()-1){
			//最后一个分类
			if(!isNext){
			bq.addWhereDoesNotExists("type");
			}else{
			bq.addWhereEqualTo("type",tt);
			}
			bq.findObjects(new FindListener<TieType>(){

					@Override
					public void done(final List<TieType> ltt, BmobException p2)
					{
						if(p2==null){
							if(ltt.size()!=0){
								if(data.get(0).getName().equals("")&&!isNext){
									data.set(0,ltt.get(0));
								}else if(isNext){
								data.add(ltt.get(0));
								}
								sx();
								ListView ll=du.dialogl1(true,title,new TieTypeAdapter(context,ltt));
								ll.setOnItemClickListener(new AdapterView.OnItemClickListener(){

										@Override
										public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
										{
											data.set(data.size()-1,ltt.get(p3));
											sx();
											tie_type(ltt.get(p3).getName(),ltt.get(p3),data.size()-1,true);
											// TODO: Implement this method
										}
									});
								
							}else{
								du.dis();
							}
						}else{
							du.dis();
							gj.show("加载失败"+p2);
						}
						
						// TODO: Implement this method
					}
				});
					
			
		}else{
			
			//中间分类
			if(position==0){
				bq.addWhereDoesNotExists("type");
			}else{
			bq.addWhereEqualTo("type",tt.getType());
			}
			bq.findObjects(new FindListener<TieType>(){

					@Override
					public void done(final List<TieType> ltt, BmobException p2)
					{
						if(p2==null){
							
							if(ltt.size()!=0){
								ListView ll=du.dialogl1(true,title,new TieTypeAdapter(context,ltt));
								ll.setOnItemClickListener(new AdapterView.OnItemClickListener(){

										@Override
										public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
										{
											
											int i=data.size()-1;
											for(;i>position;i--){
												data.remove(data.size()-1);
												Log.e("删除",position+"删除"+i);
											}
											
											data.set(data.size()-1,ltt.get(p3));
											sx();
											tie_type(ltt.get(p3).getName(),ltt.get(p3),data.size()-1,true);
											// TODO: Implement this method
										}
									});
							}else{
								for(int i=data.size()-1;i>=position;i--){
									data.remove(data.size()-1);
								}
								sx();
							}
								
				
						}else{
							du.dis();
							gj.show("加载失败"+p2);
							//gj.toastBmobException(listView,"加载失败",p2);
						}
						// TODO: Implement this method
					}
				});
		}
		
		
		// TODO: Implement this method
	}

}
